<template>
<div>
    <!-- 面包屑 -->
    <a-breadcrumb style="margin: 16px 0">
        <a-breadcrumb-item>Users</a-breadcrumb-item>
        <a-breadcrumb-item>Blog</a-breadcrumb-item>
        <a-breadcrumb-item><a @click='tohome'>home</a></a-breadcrumb-item>
        <a-breadcrumb-item>blogManage</a-breadcrumb-item>
    </a-breadcrumb>
    <a-table :columns="columns" :data-source="data">
    <a slot="action" slot-scope="text" href="javascript:;">{{text}}</a>
    <span slot="action" slot-scope="text">
        <a @click="browser(text.number,text.title)">查看</a>
        <a-divider type="vertical" />
        <a @click="deleteBlog(text.author,text.title)">删除</a>
    </span>
  </a-table>
</div>

</template>
<script>
const columns = [
  { title: '编号', dataIndex: 'number', key: 'number' },
  { title: '用户名', dataIndex: 'author', key: 'author' },
  { title: '主题', dataIndex: 'type', key: 'type' },
  { title: '博客名', dataIndex: 'title', key: 'title' ,scopedSlots: { customRender: 'name'},},
  { title: '发布时间', dataIndex: 'publishTime', key: 'publishTime' },
  { title: '操作', dataIndex: '', key: 'x', scopedSlots: { customRender: 'action'},},
];

const data = [
  {
    key: 1,
    number: 1,
    project: '生活',
    name: '吸烟为什么会危害健康',
  },
    {
    key: 2,
    number: 2,
    project: '学习',
    name: '三天打好java基础',
  },
    {
    key: 3,
    number: 3,
    project: '学习',
    name: '一天学会python',
  },
    {
    key: 4,
    number: 4,
    project: '社交',
    name: '舍友在宿舍行为不举怎么办',
  },
];

export default {
  data() {
    return {
      data,
      columns,
      id:[]
    };
  },
  created () {
    this.show()
  },
  methods:{
      tohome(){
          this.$router.push('/manage/home')
          window.sessionStorage.setItem('activepath','/manage/home')
      },
    //   tomodify(){
    //       this.$router.push('/modify')
    //   },
      browser(number,title){
          window.sessionStorage.setItem('number',this.id[number-1])
        window.sessionStorage.setItem('title',title)
          this.$router.push('/blog')
      },
      async show(){
        const{data:res} = await this.$http.get('http://localhost:8081/blog/browserallblog')
        this.data = res.data
              this.data.sort(function(a,b){
   //降序
      //  return a.time < b.time ? 1 : -1
         return a.publishTime < b.publishTime ? 1 : -1
   });
        for(var i =0;i<res.data.length;i++){
           this.id.push( this.data[i].number)
          this.data[i].number=i+1
        }
      },
      async deleteBlog(author,title){
        const {data:res} = await this.$http.get('http://localhost:8081/blog/deleteblog',{params:{author:author,title:title}})
        if(res.code == 200){
          this.$message.success('删除成功')
          this.show()
        }
        else{
          this.$message.errot('删除失败')
        }
      }
  }
};
</script>